package com.example.mz.dynamicdb.controller;

import com.example.mz.dynamicdb.bean.User;
import com.example.mz.dynamicdb.entity.Company;
import com.example.mz.dynamicdb.entity.School;
import com.example.mz.dynamicdb.entity.Student;
import com.example.mz.dynamicdb.mapper.StudentMapper;
import com.example.mz.dynamicdb.service.CompanyService;
import com.example.mz.dynamicdb.service.SchoolService;
import com.example.mz.dynamicdb.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class DemoController {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private SchoolService schoolService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentMapper studentMapper;

    @PostMapping("/getAll")
    public Map<String, Object> login() {

        Map<String, Object> returnMap = new HashMap<>();
        List<Company> companyList = companyService.getCompanyList();
        List<School> schoolList = schoolService.getSchoolList();
        List<Student> studentList = studentService.getStudentList();
        returnMap.put("companyList",companyList);
        returnMap.put("schoolList",schoolList);
        returnMap.put("studentList",studentList);
        System.out.println("log mazhao");
        return returnMap;
    }

    @PostMapping("/add")
    @Transactional
    public Map<String, Object> add(@RequestBody Map<String, String> requestMap) {

        Student student = new Student();
        student.setAge(18);
        student.setName("曹操");

        School school = new School();
        school.setClassName("火箭班");
        school.setStudentNumber(20);

        Company company =  new Company();
        company.setCompanyName("京东集团");
        company.setEmployeeNumber(20);

        companyService.addCompany(company);
        schoolService.addSchool(school);
        studentService.addStudent(student);

        return null;
    }


    @PostMapping("/batchAddStudent")
    public Map<String, Object> batchAddStudent(@RequestBody Map<String, String> requestMap) {

        List<Student> studentList = new ArrayList<>();
        for (int i = 1; i <= 100000; i++) {
            Student student = new Student();
            student.setAge(i);
            student.setName("曹操" + i);
            studentList.add(student);
        }


        studentService.batchAddStudent(studentList);

        return null;
    }

    @PostMapping("/selectStudent")
    public Map<String, Object> selectStudent(@RequestBody Map<String, String> requestMap) {

        String sex = requestMap.get("sex");

        Map<String, Object> map = new HashMap<>();
        List<Student> users = studentMapper.selectByMap(map);
        HashMap<String, Object> returnMap = new HashMap<>();
        returnMap.put("users",users);
        return returnMap;
    }
}
